import 'package:common_utils/common_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_penting.dart';
import 'package:flutter_learn_vi/src/bean/bean_user.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/utils/date_utils.dart';
import 'package:flutter_learn_vi/src/utils/navigator_utils.dart';

import 'PendingDetailsPage.dart';
import 'PendingHeaderWidget.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2020/12/25.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class PendingListItemWidget extends StatefulWidget {
  final PentingBean pentingBean;
  final bool isShowCreateTime;
  final bool isShowBottomCount;

  PendingListItemWidget({
    @required this.pentingBean,
    this.isShowCreateTime = false,
    this.isShowBottomCount = true,
  });

  @override
  _PendingListItemWidgetState createState() => _PendingListItemWidgetState();
}

class _PendingListItemWidgetState extends State<PendingListItemWidget> {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        if (widget.isShowCreateTime) {
          return;
        }
        NavigatorUtils.pushPage(
            context,
            PendingDetailsPage(
              pentingBean: widget.pentingBean,
            ), callback: (value) {
          if (value != null) {
            setState(() {});
          }
        });
      },
      child: Container(
        padding: EdgeInsets.only(left: 16, right: 16, top: 20),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: [
            buildHeaderRow(),
            buildContent(),
            buildHeaderTime(),
            Divider(),
          ],
        ),
      ),
    );
  }

  Widget buildHeaderRow() {
    UserBean userBean = widget.pentingBean.userBean;

    return PendingHeaderWidget(
      userName:
          "${userBean.mobile.substring(0, 3)}***${userBean.mobile.substring(7, 11)}",
      pentingBean: widget.pentingBean,
    );
  }

  Widget buildHeaderTime() {
    String createTime = widget.pentingBean.createTime;
    String tileLine = TimelineUtil.formatByDateTime(
        ViDateUtils.getDateTime(createTime),
        locale: "zh");
    return Row(
      mainAxisAlignment: MainAxisAlignment.end,
      children: [
        Container(
          alignment: Alignment.centerRight,
          margin: EdgeInsets.only(top: 4),
          child: Text(
            "$tileLine",
            style: TextStyle(fontSize: 12, color: Colors.blueGrey),
          ),
        )
      ],
    );
  }

  Widget buildContent() {
    String content = widget.pentingBean.pContent;
    return Container(
      margin: EdgeInsets.only(top: 12, bottom: 20),
      child: Text(
        "$content",
        style: TextStyle(fontSize: 16, color: Colors.black),
      ),
    );
  }

  Widget buildCountWidget() {
    int shareCount = widget.pentingBean.shareCount;
    int pariseCount = widget.pentingBean.pariseCount;
    int reviewCount = widget.pentingBean.reviewCount;

    int shareId = widget.pentingBean.shareId;
    int pariseId = widget.pentingBean.pariseId;
    int reviewId = widget.pentingBean.reviewId;

    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        // buildLeftAndRightWidgt(
        //     asset: shareId == null
        //         ? "assets/images/2.0x/shareCount.png"
        //         : "assets/images/2.0x/shareCount2.png",
        //     title: "$shareCount",
        //     onTap: () {
        //       ToastUtils.showToast("已助力");
        //       if (shareId == null) {
        //         addPend(2);
        //       }
        //     }),
        // buildLeftAndRightWidgt(
        //     asset: pariseId == null
        //         ? "assets/images/2.0x/pariseCount.png"
        //         : "assets/images/2.0x/pariseCount2.png",
        //     title: "$pariseCount",
        //     onTap: () {
        //       ToastUtils.showToast("已点赞");
        //       if (pariseId == null) {
        //         addPend(1);
        //       }
        //     }),
        buildLeftAndRightWidgt(
            asset: reviewId == null
                ? "assets/images/2.0x/reviewCount.png"
                : "assets/images/2.0x/reviewCount2.png",
            title: "$reviewCount")
      ],
    );
  }

  Widget buildLeftAndRightWidgt({String asset, String title, Function onTap}) {
    return Expanded(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          GestureDetector(
            onTap: onTap,
            child: Row(
              children: [
                Image.asset(
                  asset,
                  width: 18,
                  height: 18,
                  fit: BoxFit.fill,
                ),
                SizedBox(
                  width: 4,
                ),
                Text(
                  "$title",
                  style: TextStyle(fontSize: 12, color: Colors.blueGrey),
                ),
              ],
              mainAxisSize: MainAxisSize.min,
            ),
          )
        ],
      ),
    );
  }

  ///1 点赞  2 助力  3 评论
  Future<bool> addPend(int type) async {
    Map<String, dynamic> map = {
      "tpye": type,
      "pId": widget.pentingBean.id,
    };
    ResponseInfo partResponseInfo = await DioUtils.instance.postRequest(
      url: HttpHelper.pending_helper_add,
      jsonMap: map,
    );
    if (partResponseInfo.success) {
      LogUtil.e("添加成功");
      NavigatorUtils.pop(context);
    } else {
      LogUtil.e("添加失败");
    }

    return Future.value(true);
  }
}
